package com.example.xd.android_vresion_test.android_10;

import android.Manifest;
import android.content.ContentResolver;
import android.content.ContentUris;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.GridLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.bumptech.glide.Glide;
import com.chad.library.adapter.base.BaseQuickAdapter;
import com.chad.library.adapter.base.BaseViewHolder;
import com.example.xd.android_vresion_test.R;

import java.util.ArrayList;
import java.util.List;

import pub.devrel.easypermissions.EasyPermissions;

public class ShowActivity extends AppCompatActivity implements EasyPermissions.PermissionCallbacks {
    String[] permissions = {Manifest.permission.READ_EXTERNAL_STORAGE};


    private RecyclerView mRecycle_view;
    ArrayList<String> imgs = new ArrayList<String>();
    private int mWidth;
    private Adapter mAdapter;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_show);
        if (EasyPermissions.hasPermissions(this, permissions)) {
            media_test(0);
        } else {
            EasyPermissions.requestPermissions(this, "读写权限", 1, permissions);
        }
        mWidth = getWindow().getWindowManager().getDefaultDisplay().getWidth() / 4;
        mRecycle_view = findViewById(R.id.recycle_view);
        mRecycle_view.setLayoutManager(new GridLayoutManager(this, 4));
        mAdapter = new Adapter(R.layout.show_item, imgs);
        mRecycle_view.setAdapter(mAdapter);

    }

    private void media_test(int type) {
        ContentResolver contentResolver = getContentResolver();
        Cursor cursor = null;
        switch (type) {
            case 0://图片
                cursor = contentResolver.query(
                        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
                        null,
                        null,
                        null,
                        "");
                break;
            case 1://视频
                cursor = contentResolver.query(
                        MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
                        null,
                        null,
                        null,
                        "");
                break;
            case 2://音频
                cursor = contentResolver.query(
                        MediaStore.Audio.Media.EXTERNAL_CONTENT_URI,
                        null,
                        null,
                        null,
                        "");
                break;

        }

        if (cursor != null) {
            while (cursor.moveToNext()) {
                long id = cursor.getLong(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns._ID));
                Uri uri = ContentUris.withAppendedId(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, id);
                Log.i("xd---", uri.toString());
                imgs.add(uri.toString());
            }
            cursor.close();
        }
    }

    @Override
    public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
        super.onRequestPermissionsResult(requestCode, permissions, grantResults);
        EasyPermissions.onRequestPermissionsResult(requestCode, permissions, grantResults, this);
    }

    @Override
    public void onPermissionsGranted(int requestCode, @NonNull List<String> perms) {
        media_test(0);
    }

    @Override
    public void onPermissionsDenied(int requestCode, @NonNull List<String> perms) {
        Toast.makeText(this, "权限申请失败", Toast.LENGTH_SHORT).show();

    }

    class Adapter extends BaseQuickAdapter<String, BaseViewHolder> {

        public Adapter(int layoutResId, @Nullable List<String> data) {
            super(layoutResId, data);
        }

        @Override
        protected void convert(BaseViewHolder helper, String item) {
            ImageView img = helper.getView(R.id.show_item_img);
            ViewGroup.LayoutParams layoutParams = img.getLayoutParams();
            layoutParams.width = mWidth;
            layoutParams.height = mWidth;
            img.setLayoutParams(layoutParams);
            Glide.with(ShowActivity.this).load(item).into(img);
        }
    }
}
